package com.sina.shop.manage.controller.carousel;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.web.BaseController;
import com.sina.shop.domain.carousel.Carousel;
import com.sina.shop.manage.service.carousel.CarouselManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-12-07-19:00
 */
@Controller
@RequestMapping("carouselManage")
public class CarouselManageController extends BaseController {

    @Autowired
    @Qualifier("carouselManageService")
    private CarouselManageService carouselManageService;

    @RequestMapping(value = "/carouselList", method = RequestMethod.GET)
    public ModelAndView getCarouselList(@Validated Carousel carousel) {
        LOG.info("carousel:{}", carousel);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];

        List<Map<String, Object>> carouselList = carouselManageService.getCarouselList(carousel, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", carouselList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "deleteCarousel", method = RequestMethod.POST)
    public ModelAndView deleteCarousel(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            carouselManageService.deleteCarousel(id);
            result.put(CODE, OK);
            result.put(MSG, "删除文件成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "deleteCarouselTab", method = RequestMethod.POST)
    public ModelAndView deleteCarouselTab(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            carouselManageService.deleteCarouselTab(id);
            result.put(CODE, OK);
            result.put(MSG, "删除文件成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "updateCarousel", method = RequestMethod.POST)
    public ModelAndView updateCarousel(@Validated Carousel carousel) {

        LOG.info("carousel:{}", carousel);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            carouselManageService.updateCarousel(carousel);
            result.put(CODE, OK);
            result.put(MSG, "更新文件成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/carouselListToHtml", method = RequestMethod.GET)
    public ModelAndView getCarouselListToHtml() {
        ModelAndView mv = new ModelAndView("carousel/carouselList");

        return mv;
    }
}
